<?php
/**
 * 
 */
namespace app\home\controller\bank;

use app\home\controller\Base;

class Handle extends Base
{

	private $params;

	public function __construct(){
		parent::__construct();
		// token验证
		if(false == Base::checkToken()){
			$this->error('token error!');
		}
		// 参数去token
		$params = $this -> request -> post();
		unset($params['token']);
		$this->params = $params;
	}

	/**
	 * 添加银行卡
	 */
	public function add(){

		$params = $this->params;
		$validate = $this->validate($params,'Bank.home_add');
		if(true!==$validate){
			$this->error($validate);
		}
		
		$model = model('member_bank_card');
		$data = [
			'uid' => UID,
			'bank_id' => $params['bank_id'],
			'bank_address' => $params['bank_address'],
			'bank_card' => $params['bank_card'],
			'owner' => $params['owner'],
		];

		if(false===$model->addBank($data)){
			$this->error($model->getError());
		}else{
			$this->success('添加成功');
		}

	}

	/**
	 * 设置默认银行卡
	 */
	public function set_default(){
		$params = $this->params;

		$validate = $this->validate($params,'Bank.home_default');
		if(true!==$validate){
			$this->error($validate);
		}
		$id = $params['id'];
		
		$model = model('member_bank_card');
		if(false===$model->setDefault($id,UID)){
			$this->error($model->getError());
		}else{
			$this->success('设置成功');
		}
	}

}